home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11809 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  91 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: "free"ing classes when the "new" is inside a function
  5. Message-ID: <marnoldDoCs2M.E00@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4ic7a5$94u@falcon.ccs.uwo.ca> <3149B5B8.5C25@bnr.ca> <4icna3$7p3@falcon.ccs.uwo.ca>
  8. Date: Sat, 16 Mar 1996 09:08:46 GMT
  9. Sender: marnold@netcom2.netcom.com
  10.  
  11. turnbull@canlon.physics.uwo.ca (David Turnbull) writes:
  12.  
  13. >Joseph Bell <jobell@bnr.ca> wrote:
  14.  
  15. [snip]
  16.  
  17. >>What is the difference between MyClass and Class?
  18.  
  19. >>Consider:
  20.  
  21. >>A* returnPtrtoA()
  22. >>{
  23. >>  A* returnPtr = new A(...);
  24. >>  return returnPtr;
  25. >>}
  26.  
  27. >>void main (void)
  28. >>{
  29. >>  A* myAPtr = returnPtrtoA();
  30. >>  
  31.  
  32. >>  delete myAPtr;  // frees object and calls destructor
  33. >>}
  34.  
  35. >>That works.
  36.  
  37. >Sorry, I got careless in my example.  Class and MyClass were supposed
  38. >to be the same. In other words, like your example. However, this does
  39. >NOT free up the memory!
  40. >Or perhaps my simplification of the problem is hiding something. What
  41. >I have is closer to
  42.  
  43.  
  44. >MyClass*  Level1()
  45. >{
  46. >     MyClass* result;
  47. >    result=Level2();
  48. >    return result;
  49. >}
  50.  
  51. >MyClass*  Level2()
  52. >{
  53. >     MyClass* result;
  54. >    result=Level3();
  55. >    return result;
  56. >}
  57.  
  58. >MyClass*  Level3()
  59. >{
  60. >     MyClass* result;
  61. >    result=new MyClass(..........);
  62. >    return result;
  63. >}
  64.  
  65. >void main(void)
  66. >{
  67.  
  68. >   MyClass* value;
  69. >   value=Level1();
  70. >   delete value;
  71.  
  72. >}
  73.  
  74. >In this case the delete is not freeing memory.
  75.  
  76. It certainly should!  delete does not care where the new occured.  Your
  77. code allocated a MyClass object and stores it in a MyClass* variable, 
  78. eventually using such a varaible to delete the allocated MyClass object.
  79. Perfectly wonderful C++.
  80.  
  81. What means are you using to determine that memory is not being freed?
  82.  
  83. Regards,
  84. -------------------------------------------------------------------------
  85. Matt Arnold                       |        | ||| | |||| |  | | || ||
  86. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  87. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  88. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  89. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  90. -------------------------------------------------------------------------
  91.